package back.action;
import back.service.BackService;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import com.alibaba.fastjson.JSON;
import common.servlet.BaseServlet;
import common.spring.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 马泽朋
 * @version 1.0
 * @date 2019/10/19 18:43
 */
@WebServlet("/backstage.action")
public class Back extends BaseServlet {
    private static final long serialVersionUID = -3615192370238494739L;
    private static BackService backService;
    private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "spring/ApplicationContext-service.xml");
    static {
        try {
            backService = (BackService) context.getBean(BackService.class.getSimpleName());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 登录模块
     * @param request 请求
     * @param response 响应
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //拿到请求发送的账号
        String username = request.getParameter("username");
        //密码
        String password = request.getParameter("password");
        Map<String, Object> map = new HashMap<>();
        map.put("username",username);
        map.put("password",password);
        //调用service的方法验证登录是否成功，返回的map中包含管理员对象
        Map<String, Object> map1 = backService.loginbackgroundByaccountAndpassword(map);
        if(map1.containsKey("token")){
            request.getSession().setAttribute(username, map.get("token"));
        }
        //把数据打包发送到前端
        printJson(request, response, map1);
    }

    /**
     * 主页显示小说数量信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void homeInformation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String s = backService.homePageInformationQuery();
        response.getWriter().println(s);
    }

    /**
     * 显示小说列表
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void novelist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //page=3&limit=10
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", limit);
        Map<String, Object> novelist = backService.novelist(map);
        response.getWriter().println(JSON.toJSONString(novelist));
        response.getWriter().close();
    }
}
